﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TranscoderSharp;

namespace Enceladus___Audio_Transcoder
{
    public partial class LameConfigUI : UserControl
    {
        LameConfig m_LameConfig = LameConfig.Load();

        public LameConfigUI()
        {

            InitializeComponent();

            LoadFromConfig();
        }

        ~LameConfigUI()
        {
            //SaveToConfig();
        }

        public void LoadFromConfig()
        {
            checkBox_Mono.Checked = m_LameConfig.MonoEncoding;
            checkBox_Copyright.Checked = m_LameConfig.Copyright;
            checkBox_Original.Checked = m_LameConfig.Original;
            label_Bitrate.Text = m_LameConfig.Bitrate.ToString();

            radioButton_bitrate.Checked = (m_LameConfig.EncodingTarget == LameConfig.Target.Bitrate);
            radioButton_quality.Checked = (m_LameConfig.EncodingTarget == LameConfig.Target.Quality);

            groupBox_bitrate.Enabled = (m_LameConfig.EncodingTarget == LameConfig.Target.Bitrate);
            groupBox_Quality.Enabled = !groupBox_bitrate.Enabled;
            
            trackBar_Bitrate.Value = (m_LameConfig.Bitrate / 32);
            trackBar_Quality.Value = m_LameConfig.VariableBitrateMode;
            label_Quality.Text = "Quality " + m_LameConfig.VariableBitrateMode.ToString();

            comboBox_EncodingEngineQuality.SelectedText = m_LameConfig.EngineQuality.ToString();

            comboBox_BitrateType.SelectedText = m_LameConfig.BitrateEncodingType.ToString();
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_bitrate.Enabled = radioButton_bitrate.Checked;
            groupBox_Quality.Enabled = !radioButton_bitrate.Checked;
        }

        private void trackBar_Bitrate_Scroll(object sender, EventArgs e)
        {
            label_Bitrate.Text = (trackBar_Bitrate.Value * 32).ToString();
        }

        public void SaveToConfig()
        {
            m_LameConfig.MonoEncoding = checkBox_Mono.Checked;
            m_LameConfig.Original = checkBox_Original.Checked;
            m_LameConfig.Copyright = checkBox_Copyright.Checked;

            m_LameConfig.Bitrate = trackBar_Bitrate.Value * 32;
            
            m_LameConfig.EncodingTarget = radioButton_bitrate.Checked ? LameConfig.Target.Bitrate : LameConfig.Target.Quality;

            m_LameConfig.EngineQuality = (LameConfig.EncodingEngineQuality)Enum.Parse(typeof(LameConfig.EncodingEngineQuality), comboBox_EncodingEngineQuality.Text);

            m_LameConfig.BitrateEncodingType = (LameConfig.BitrateType)Enum.Parse(typeof(LameConfig.BitrateType), comboBox_BitrateType.Text);
            
            //m_LameConfig.EngineQuality = comboBox_EncodingEngineQuality.SelectedText == "Standard"
            //    ? LameConfig.EncodingEngineQuality.Standard : LameConfig.EncodingEngineQuality.High;

            m_LameConfig.VariableBitrateMode = trackBar_Quality.Value;

            m_LameConfig.Save();
        }

        private void trackBar_Quality_Scroll(object sender, EventArgs e)
        {
            label_Quality.Text = "Quality " + trackBar_Quality.Value.ToString();
        }       
    }
}
